package meiyunting.shiwuzhaoling.controller;

import meiyunting.shiwuzhaoling.client.ActivityServiceClient;
import meiyunting.shiwuzhaoling.client.LibraryServiceClient;
import meiyunting.shiwuzhaoling.client.MarketServiceClient;
import meiyunting.shiwuzhaoling.model.User;
import meiyunting.shiwuzhaoling.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 集成控制器，用于整合其他微服务的数据
 */
@RestController
@RequestMapping("/api/integration")
public class IntegrationController {

    private final ActivityServiceClient activityServiceClient;
    private final MarketServiceClient marketServiceClient;
    private final LibraryServiceClient libraryServiceClient;
    private final UserService userService;

    @Autowired
    public IntegrationController(
            ActivityServiceClient activityServiceClient,
            MarketServiceClient marketServiceClient,
            LibraryServiceClient libraryServiceClient,
            UserService userService) {
        this.activityServiceClient = activityServiceClient;
        this.marketServiceClient = marketServiceClient;
        this.libraryServiceClient = libraryServiceClient;
        this.userService = userService;
    }

    /**
     * 获取用户参与的活动
     */
    @GetMapping("/user/{userId}/activities")
    public List<Map<String, Object>> getUserActivities(@PathVariable Long userId) {
        // 验证用户存在
        userService.findById(userId);
        // 调用活动服务
        return activityServiceClient.getUserActivities(userId);
    }

    /**
     * 获取用户发布的商品
     */
    @GetMapping("/user/{userId}/products")
    public List<Map<String, Object>> getUserProducts(@PathVariable Long userId) {
        // 验证用户存在
        userService.findById(userId);
        // 调用二手交易服务
        return marketServiceClient.getUserProducts(userId);
    }

    /**
     * 获取用户借阅的图书
     */
    @GetMapping("/user/{userId}/books")
    public List<Map<String, Object>> getUserBorrowedBooks(@PathVariable Long userId) {
        // 验证用户存在
        userService.findById(userId);
        // 调用图书管理服务
        return libraryServiceClient.getUserBorrowedBooks(userId);
    }

    /**
     * 获取用户仪表盘数据（整合多个服务的数据）
     */
    @GetMapping("/user/{userId}/dashboard")
    public Map<String, Object> getUserDashboard(@PathVariable Long userId) {
        Map<String, Object> dashboard = new HashMap<>();
        
        try {
            // 获取用户信息
            User user = userService.findById(userId);
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("id", user.getId());
            userInfo.put("username", user.getUsername());
            userInfo.put("email", user.getEmail());
            dashboard.put("user", userInfo);
            
            try {
                // 获取用户的活动
                List<Map<String, Object>> activities = activityServiceClient.getUserActivities(userId);
                dashboard.put("activities", activities);
            } catch (Exception e) {
                dashboard.put("activities", Collections.emptyList());
                dashboard.put("activityError", "无法获取活动信息: " + e.getMessage());
            }
            
            try {
                // 获取用户的二手商品
                List<Map<String, Object>> products = marketServiceClient.getUserProducts(userId);
                dashboard.put("products", products);
            } catch (Exception e) {
                dashboard.put("products", Collections.emptyList());
                dashboard.put("productError", "无法获取商品信息: " + e.getMessage());
            }
            
            try {
                // 获取用户的借阅图书
                List<Map<String, Object>> books = libraryServiceClient.getUserBorrowedBooks(userId);
                dashboard.put("books", books);
            } catch (Exception e) {
                dashboard.put("books", Collections.emptyList());
                dashboard.put("bookError", "无法获取图书信息: " + e.getMessage());
            }
        } catch (Exception e) {
            dashboard.put("error", "获取用户信息失败: " + e.getMessage());
        }
        
        return dashboard;
    }
} 